/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views;

import Common.ConnectionDB;
import DAO.CategoryDAO;
import Helper.CategoryHelper;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.ResultSet;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnitSource;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RefineryUtilities;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a bar chart.
 *
 */
public class ReportChart extends javax.swing.JDialog {
    
    JComboBox cbbThang, cbbNam, cbbCategory;
    CategoryHelper cateData;
    ResultSet rs = null, rs1 = null;
    HashMap mapStore;
    private ResourceBundle rb;
    public static ReportChart demo;
    CategoryDataset dataset, datasettpm;
    JPanel vPanel;
    JFreeChart chart;
    ChartPanel chartPanel;

    /**
     * Creates a new demo instance.
     *
     * @param title the frame title.
     */
    
    public ReportChart() {
    }

    
    public ReportChart(final String title, int month, int year) {
        //   final CategoryDataset dataset = createDataset();

        vPanel = new JPanel();
        Date current = new Date();
        int currentmonth = current.getMonth();
         int currentyear = current.getYear();
        String thang[] = {"Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6", "Tháng 7", "Tháng 8",
            "Tháng 9", "Tháng 10", "Tháng 11", "Tháng 12"};
         String[] nam = new String[200];
        int index = 0;
        for (int i = 2013; i < 2099; i++) {
            nam[index] = "" + i;
            index++;
        }
        cbbNam = new JComboBox(nam);
       
        cbbThang = new JComboBox(thang);
        if (month == 0 && year == 0) {
            cbbThang.setSelectedIndex(currentmonth);
            cbbNam.setSelectedItem(currentyear);
        } else {
            cbbThang.setSelectedIndex(month);
            cbbNam.setSelectedIndex(year);
        }
        vPanel.add(cbbThang);
         vPanel.add(cbbNam);
        cbbThang.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                int t = (int) cbbThang.getSelectedIndex();
                System.out.print(t);
                
            }
        });
        
       
        
        
        cbbNam.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                String nam = (String) cbbNam.getSelectedItem();
                System.out.print(nam);
            }
        });
        dataset = createDataset();
        JButton addButton = new JButton("View");
        vPanel.add(addButton);
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dataset = createDataset();
                chart = createChart(dataset);
                chartPanel = new ChartPanel(chart, false);
                chartPanel.setPreferredSize(new Dimension(640, 480));
                add(chartPanel, BorderLayout.CENTER);
                // this.setVisible(false);
                int t = (int) cbbThang.getSelectedIndex();
                int n = (int) cbbNam.getSelectedIndex();
//                demo.removeAll();
                demo.setVisible(false);
                demo = new ReportChart("Bar Chart Demo", t, n);
                demo.pack();
                RefineryUtilities.centerFrameOnScreen(demo);
                demo.setVisible(true);

                // demo.add(chartPanel, BorderLayout.CENTER);
            }
        });
        
        this.add(vPanel, BorderLayout.SOUTH);
//        this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);

        
        chart = createChart(dataset);
        chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(640, 480));
        this.add(chartPanel, BorderLayout.CENTER);
        
    }
    /**
     * Returns a sample dataset.
     *
     * @return The dataset.
     */
    String intTT = 0 + "";
    
    private CategoryDataset createDataset() {
        // row keys...
        final String series1 = "First";
        String year = (String) cbbNam.getSelectedItem();
        String month = cbbThang.getSelectedIndex() + 1 + "";
        // column keys...

        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        try {
            rs = CategoryDAO.getCategory(ConnectionDB.ConnectionDatabase());

            mapStore = new HashMap();
            while (rs.next()) {
                String strId = rs.getString("CatID");
                String strName = rs.getString("CatName");
                
                rs1 = CategoryDAO.TTchart(ConnectionDB.ConnectionDatabase(), month, year, strId);
                  System.out.println(strName);
                while (rs1.next()) {
                    intTT = rs1.getString("tongtien");
                    System.out.println("tong so tien da ban:" + intTT);
                    if (intTT == null) {
                        intTT = 0 + "";
                        System.out.println("tong so tien da ban:" + intTT);
                    }
                    
                }
//                CategoryHelper roletemp = new CategoryHelper(strId, strName);
//                mapStore.put(strId, roletemp);
                dataset.addValue(Double.parseDouble(intTT), series1, rs.getString("CatName"));
            }
//            cbbCategory.setModel(cboCate);
        } catch (Exception ex) {
            try {
                   System.out.println("why?");
                ConnectionDB.CloseDB(rs);
            } catch (Exception ex1) {
                Logger.getLogger(Users.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        
        
        return dataset;
        
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset the dataset.
     *
     * @return The chart.
     */
    private JFreeChart createChart(final CategoryDataset dataset) {

        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(
                "Bar Chart Demo", // chart title
                "Category", // domain axis label
                "Value", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips?
                false // URLs?
                );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);

        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
                0.0f, 0.0f, Color.blue,
                0.0f, 0.0f, Color.lightGray);
        final GradientPaint gp1 = new GradientPaint(
                0.0f, 0.0f, Color.green,
                0.0f, 0.0f, Color.lightGray);
        final GradientPaint gp2 = new GradientPaint(
                0.0f, 0.0f, Color.red,
                0.0f, 0.0f, Color.lightGray);
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);
        
        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
                CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
        // OPTIONAL CUSTOMISATION COMPLETED.

        return chart;
        
    }

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************
    /**
     * Starting point for the demonstration application.
     *
     * @param args ignored.
     */
    public static void main(final String[] args) {
        
        demo = new ReportChart("Bar Chart Demo", 0, 0);
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
        
    }
}
